# Copyright 2025 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@rules_rust//rust:defs.bzl", "rust_doc", "rust_library")
load("//pw_kernel:flags.bzl", "KERNEL_TEST_DEPS", "KERNEL_TEST_RUSTC_FLAGS")

package(default_visibility = ["//visibility:public"])

CORTEX_M_DEPS = [
    "@rust_crates//:cortex-m",
    "@rust_crates//:cortex-m-rt",
]

RISCV_DEPS = [
    "@rust_crates//:riscv",
    "@rust_crates//:riscv-rt",
]

rust_library(
    name = "kernel",
    srcs = [
        "arch.rs",
        "arch/arm_cortex_m.rs",
        "arch/arm_cortex_m/exceptions.rs",
        "arch/arm_cortex_m/features.rs",
        "arch/arm_cortex_m/protection.rs",
        "arch/arm_cortex_m/regs.rs",
        "arch/arm_cortex_m/regs/mpu.rs",
        "arch/arm_cortex_m/regs/msr.rs",
        "arch/arm_cortex_m/regs/nvic.rs",
        "arch/arm_cortex_m/regs/scb.rs",
        "arch/arm_cortex_m/regs/systick.rs",
        "arch/arm_cortex_m/spinlock.rs",
        "arch/arm_cortex_m/syscall.rs",
        "arch/arm_cortex_m/threads.rs",
        "arch/arm_cortex_m/timer.rs",
        "arch/host.rs",
        "arch/host/spinlock.rs",
        "arch/riscv.rs",
        "arch/riscv/exceptions.rs",
        "arch/riscv/protection.rs",
        "arch/riscv/regs.rs",
        "arch/riscv/regs/pmp.rs",
        "arch/riscv/spinlock.rs",
        "arch/riscv/threads.rs",
        "arch/riscv/timer.rs",
        "lib.rs",
        "panic.rs",
        "scheduler.rs",
        "scheduler/locks.rs",
        "scheduler/thread.rs",
        "sync.rs",
        "sync/event.rs",
        "sync/mutex.rs",
        "sync/spinlock.rs",
        "syscall.rs",
        "target.rs",
        "timer.rs",
    ],
    crate_features = ["user_space"] + select({
        "@pigweed//pw_build/constraints/arm:cortex-m0": ["arch_arm_cortex_m"],
        "@pigweed//pw_build/constraints/arm:cortex-m3": ["arch_arm_cortex_m"],
        "@pigweed//pw_build/constraints/arm:cortex-m33": ["arch_arm_cortex_m"],
        "@platforms//cpu:riscv32": ["arch_riscv"],
        "//conditions:default": [
            "arch_host",
            "std_panic_handler",
        ],
    }),
    proc_macro_deps = [
        "//pw_kernel/macros:riscv_macro",
        "//pw_kernel/macros:arm_cortex_m_macro",
        "@rust_crates//:paste",
    ],
    rustc_flags = KERNEL_TEST_RUSTC_FLAGS,
    tags = ["kernel"],
    deps = [
        "//pw_kernel/config:kernel_config",
        "//pw_kernel/lib/foreign_box",
        "//pw_kernel/lib/list",
        "//pw_kernel/lib/log_if",
        "//pw_kernel/lib/pw_assert",
        "//pw_kernel/lib/pw_cast",
        "//pw_kernel/lib/pw_status",
        "//pw_kernel/lib/regs",
        "//pw_kernel/lib/time",
        "//pw_kernel/syscall:syscall_defs",
        "@pigweed//pw_log/rust:pw_log",
    ] + select({
        "@pigweed//pw_build/constraints/arm:cortex-m0": CORTEX_M_DEPS,
        "@pigweed//pw_build/constraints/arm:cortex-m3": CORTEX_M_DEPS,
        "@pigweed//pw_build/constraints/arm:cortex-m33": CORTEX_M_DEPS,
        "@platforms//cpu:riscv32": RISCV_DEPS,
        "//conditions:default": [],
    }) + KERNEL_TEST_DEPS,
)

rust_doc(
    name = "kernel_doc",
    crate = ":kernel",
    rustdoc_flags = [
        "--document-private-items",
        "--cfg=feature=\"user_space\"",
    ] + select({
        # Arch feature selection will be removed when arch support moves out of
        # the kernel crate.
        "@pigweed//pw_build/constraints/arm:cortex-m0": ["--cfg=feature=\"arch_arm_cortex_m\""],
        "@pigweed//pw_build/constraints/arm:cortex-m3": ["--cfg=feature=\"arch_arm_cortex_m\""],
        "@pigweed//pw_build/constraints/arm:cortex-m33": ["--cfg=feature=\"arch_arm_cortex_m\""],
        "@platforms//cpu:riscv32": ["--cfg=feature=\"arch_riscv\""],
        "//conditions:default": [
            "--cfg=feature=\"arch_host\"",
            "--cfg=feature=\"std_panic_handler\"",
        ],
    }),
    tags = ["kernel"],
)
